package com.zlm.mall.publisher.controller;

import com.zlm.mall.publisher.service.ESService;
import com.zlm.mall.publisher.service.OrderWideService;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Harbour
 * @date 2021-04-08 9:53
 */
@RestController
@CrossOrigin
public class OrderWideController {

    @Autowired
    ESService esService;

    @Autowired
    OrderWideService orderWideService;

    /*
    访问接口 ： http://localhost:8070/realtime-total?date=2021-4-25
    数据格式 ： [{"name":"xx", "id":"xxx", "value":xxx}, {"name":"xx", "id":"xxx", "value":xxx}]
    */
    @RequestMapping("/realtime-total")
    public Object realtimeTotal(@RequestParam(value = "date", defaultValue = "dau") String date) {
        ArrayList<HashMap<String, String>> totalList = new ArrayList<>(2);
        HashMap<String, String> dauMap = new HashMap<>();
        HashMap<String, String> userMap = new HashMap<>();
        HashMap<String, String> amountMap = new HashMap<>();

        dauMap.put("name", "新增日活");
        dauMap.put("id", "dau");
        dauMap.put("value", esService.getDauTotal(date).toString());

        userMap.put("name", "新增用户");
        userMap.put("id", "user");
        userMap.put("value", esService.getNewUserTotal(date).toString());

        amountMap.put("name", "新增交易额");
        amountMap.put("id", "amount");
        amountMap.put("value", orderWideService.selectOrderAmountTotal(date).toString());

        totalList.add(dauMap);
        totalList.add(userMap);
        totalList.add(amountMap);

        return totalList;
    }

    /*
    访问接口 ： http://localhost:8070/realtime-hour?date=2020-08-18
    数据格式 ： [ yesterday: {"name":"xx", "id":"xxx", "value":xxx},
                  today: {"name":"xx", "id":"xxx", "value":xxx}]
    */
    @RequestMapping("/realtime-hour")
    public Object realtimeHour(@RequestParam("id") String id, @RequestParam("date") String date) {
        if ("dau".equalsIgnoreCase(id)) {
            Map<String, Map<String, Long>> dauHourMap = new HashMap<>();
            dauHourMap.put("yesterday", esService.getDauHour(getYesterday(date)));
            dauHourMap.put("today", esService.getDauHour(date));
            return dauHourMap;
        } else if ("amount".equalsIgnoreCase(id)) {
            return orderWideService.selectOrderAmountHourMap(date);
        }
        return null;
    }

    @RequestMapping("/realtime-month")
    public Object getMonthTotalAmount(@RequestParam("n") int latestN) {
        return orderWideService.selectOrderAmountMonthMap(latestN);
    }

    @RequestMapping("/realtime-province")
    public Object getProvinceTotalAmount(@RequestParam("year") int year) {
        return orderWideService.selectOrderAmountProvinceMap(year);
    }

    @RequestMapping("/realtime-hot")
    public Object getHotTrademarkAmount(@RequestParam("topN") int topN) {
        return orderWideService.selectHotTradeMarkMap(topN);
    }

    @RequestMapping("/realtime-compare")
    public Object getOrderAmountComparison() {
        return orderWideService.selectOrderAmountComparisonMap();
    }

    @RequestMapping("/realtime-week")
    public Object getOrderAmountWeek() {
        return orderWideService.selectOrderAmountWeekMap();
    }

    @RequestMapping("/realtime-age")
    public Object getUserAgeGroup() {
        return orderWideService.selectUserAgeGroupMap();
    }

    @RequestMapping("/realtime-orderCount")
    public Object getOrderCount() {
        return orderWideService.selectOrderCountMap();
    }

    /**
     * 将今日日期转为昨日
     * @param date 今天日期
     * @return 昨天日期
     */
    private String getYesterday(String date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date yesterday = null;
        try {
            Date today = simpleDateFormat.parse(date);
            yesterday = DateUtils.addDays(today, -1);
        } catch (ParseException e) {
            e.printStackTrace();
            throw new RuntimeException("日期转换失败");
        }
        return simpleDateFormat.format(yesterday);
    }
}
